import Mock, { mock } from "mockjs";
// var Mock = require('mockjs')
var Random = Mock.Random;

//道路站点
const data = Mock.mock({
  "data|20": [
    {
      "id|+1": 0, //商品id
      "nickname|1": ["运城区", "黄石区", "浦东区", "大雁区"], //所属人
      "asidekind|1": ["山西省", "湖北省", "上海市", "河北省", "陕西省"],
      kind: "@csentence(5)",
      kind11: "@csentence(8)",
      desc: Random.cparagraph(),
      time: '@datetime("2022-MM-dd A HH:mm:ss")',
    },
  ],
});

// 获取表单渲染页面
Mock.mock("/operation", "get", (comfig) => {
  return data;
});
//添加功能
Mock.mock("/operation/add", "post", (comfig) => {
  const data1 = JSON.parse(comfig.body);
  data1.id = data.data.length + 1;
  data.data.push(data1);
  return data;
});
//查询功能

Mock.mock("/operation/search", "post", (comfig) => {
  const data1 = JSON.parse(comfig.body);
  const data2 = data.data.filter((item) => {
    return (
      item.asidekind === data1.asidekind || item.nickname === data1.nickname
    );
  });
  return data2;
});
//删除功能
Mock.mock("/operation/remove", "post", (comfig) => {
  const data1 = JSON.parse(comfig.body);
  data.data.forEach((item,index)=>{
      if(item.id===data1.id){
        data.data.splice(index,1)
      }
  })
  return data;
});
///operation/set 设置当前数据
Mock.mock("/operation/set", "post", (comfig) => {
  const data1 = JSON.parse(comfig.body);
  console.log(data1);
  data.data.forEach((item,index) => {
    if(item.id===data1.id){
      console.log(index);
      data.data.splice(index,1,data1)
    }
  });
  
  return data
});
///operation/set 查看当前数据
Mock.mock("/operation/wacth", "post", (comfig) => {
  const data1 = JSON.parse(comfig.body);
  console.log(data1);

  data.data.map((item)=>{
    return item.id===data1.id
  })
});
